#!/bin/sh

[ -z "$JAVA_HOME" ] && echo "JAVA_HOME is currently not set. This script will not work." && exit 1
[ ! -f "$JAVA_HOME/bin/native-image" ] && echo "Can't find native-image in $JAVA_HOME. Check that the directory is valid." && exit 1
[ ! -f "$JAVA_HOME/jre/lib/rt.jar" ] && echo "This demo works only with Java 8. Set your JAVA_HOME to a JDK 8 version." && exit 1
[ -z "$SCALA_HOME" ] && echo "SCALA_HOME is currently not set. This script will not work." && exit 1
SCALA_LIB="$SCALA_HOME/lib"
if [ -d "$SCALA_HOME/libexec" ]; then
   SCALA_LIB="$SCALA_HOME/libexec/lib"
fi
[ ! -d "$SCALA_LIB" ] && echo "Can't find jars in $SCALA_LIB. Check that the Scala instalation is correct." && exit 1
for filename in $SCALA_LIB/*.jar; do
    SCALA_LIB_CLASSPATH=$filename:$SCALA_LIB_CLASSPATH
done

./scalac -classpath '""' -Dscala.boot.class.path=$SCALA_LIB_CLASSPATH:$JAVA_HOME/jre/lib/rt.jar -Dscala.home=$SCALA_HOME -Dscala.usejavacp=true -Denv.emacs= $@
